-- eliminate
-- create by zhuangsl
-- 生命低于一定百分比时斩杀（BOSS无效），否则造成伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        local prop;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        -- 计算是否斩杀
        local killRate = para[3] + para[4] * level;
        local hp = target:queryAttrib("hp");
        local maxHp = target:queryAttrib("max_hp");
        local damage;

        if (target:isBoss()) then
            killRate = math.modf(para[4] * level / 2);
        end

        -- 当前生命可能比最大值高，取高的
        maxHp = math.max(maxHp, hp);
        -- // 1000 * hp < kill_rate * max_hp == kill_rate * max_hp / 1000 > hp
        if FormulaM.invoke("CALC_BIG_DIVISION", killRate, maxHp, 1000) >= hp then
            if SkillM.immunitySkill(source, target, skillId) then
                -- 目标有免疫专属技属性
                if not SkillM.getSequence(target) then
                    SkillM.initSequence(source, target, skillId);
                end
                SkillM.getSequence(target):immunity(source, target, skillId);
            else
                -- 生命值低于斩杀比率时，直接杀死目标
                damage = hp;
                -- 增加命中动作
                SkillM.getSequence(target):hit(source, target, skillId);
                -- 伤害
                CombatM.doDamage(source, target, damage, skillId, nil, extra);
            end
        else
            -- 攻击方的攻击
            local attack = source:getAttack();

            -- 被对方百分比削弱
            prop = PropM.combine(target, "weak_enemy", "attack");
            attack = PropM.apply(prop, attack);

            -- 被对方绝对值削弱
            prop = PropM.combine(target, "weak_enemy2", "attack");
            attack = PropM.apply(prop, attack);

            -- 伤害
            local force = 800 + para[2] * level;
            damage = math.modf(attack * force / 1000);

            -- 斯巴达专属加成
            local prop = PropM.combine(source, "battle_cry", 1);
            if prop[3] > 0 then
                -- 触发特技发动事件
                EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
            end
            damage = PropM.apply(prop, damage);

            -- 增加命中动作
            SkillM.getSequence(target):hit(source, target, skillId);
            -- 造成伤害
            CombatM.receiveDamage(source, target, damage, skillId, extra);
        end

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]) };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 使用特殊描述
        local dbase = SkillM.query(skillId, "dbase");

        if dbase["special_scene_desc"] then
            local dungeonId = DungeonM.getDungeonId();
            local parentId  = DungeonAreaM.getParentId(dungeonId);
            local dungeonType = DungeonAreaM.query(parentId, "type");

            if dungeonType ~= "main" and dungeonType ~= "dlc" then
                desc = getSkillLocStr(dbase["special_scene_desc"]);
            end
        end

        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);

        -- 攻击方的攻击
        local attack = source:getAttack();

        local force = 800 + para[2] * level;

        local damage = math.modf(attack * force / 1000);
        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        damage = PropM.apply(prop, damage);

        desc = string.gsub(desc, "{damage}", damage);

        -- 斩杀的HP比率
        local killRate = para[3] + para[4] * level;
        desc = string.gsub(desc, "{kill_rate}", killRate / 10 );

        local bossRate = math.modf(para[4] * level / 2);
        desc = string.gsub(desc, "{boss_rate}", bossRate / 10 );

        return desc;
    end,
};
